#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/7/9 18:21
# @File    : Ademo.py
# @Description : -i http://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn


# 作者：昌龙XL
import tkinter as tk
from PIL import Image, ImageTk
from ctypes import windll
import time


# 微秒级等待
def micro_sleep(sec):
    st = time.perf_counter()
    while time.perf_counter() - st < sec:
        pass


# 窗口渐显/隐
def window_gradually_(mode='displays'):
    if mode == 'displays':
        for i in range(20):
            root.attributes("-alpha", (i + 1) / 20)
            root.update()
            micro_sleep(0.003)
        root.attributes("-alpha", 1)
    elif mode == 'hide':
        for i in range(20):
            root.attributes("-alpha", 1 - ((i + 1) / 20))
            root.update()
            micro_sleep(0.003)
        root.attributes("-alpha", 0)


root = tk.Tk()
# DPI调节，通俗点就是变清晰
windll.shcore.SetProcessDpiAwareness(1)
ScaleFactor = windll.shcore.GetScaleFactorForDevice(0)
root.tk.call('tk', 'scaling', ScaleFactor / 75)

glass_color = 'grey15'  # 可以改颜色，16进制或颜色代码（如red）
root.config(bg=glass_color)
root.geometry(f'1000x380+{root.winfo_screenwidth() // 2 - 300}+{root.winfo_screenheight() // 2 - 300}')
root.attributes('-transparentcolor', glass_color)
root.overrideredirect(True)

# 背景
WBGImg = Image.open('./img.png')
WBG = tk.Canvas(
    root, bg=glass_color, width=WBGImg.width, height=WBGImg.height, highlightthickness=0
)
WBG.pack()
WBGImg = ImageTk.PhotoImage(WBGImg)
WBG.create_image(0, 0, image=WBGImg, anchor='nw')


# 窗口移动
def MouseDown(event):
    global mousX
    global mousY
    mousX = event.x
    mousY = event.y


def MouseMove(event):
    root.geometry(f'+{event.x_root - mousX}+{event.y_root - mousY}')


root.bind('<Button-1>', MouseDown)
root.bind('<B1-Motion>', MouseMove)

# 添加到任务栏
GWL_EXSTYLE = -20
WS_EX_APPWINDOW = 0x00040000
WS_EX_TOOLWINDOW = 0x00000080


def set_appwindow(root):
    hwnd = windll.user32.GetParent(root.winfo_id())
    style = windll.user32.GetWindowLongW(hwnd, GWL_EXSTYLE)
    style = style & ~WS_EX_TOOLWINDOW
    style = style | WS_EX_APPWINDOW
    windll.user32.SetWindowLongW(hwnd, GWL_EXSTYLE, style)
    root.wm_withdraw()
    root.after(10, lambda: root.wm_deiconify())


root.after(10, lambda: set_appwindow(root=root))


# 关闭
def ClosePress(event=None):
    WBG.create_image(999, 1, image=closeB_p, anchor='ne', tag='closeB_P')


def Winquit(event=None):
    window_gradually_(mode='hide')
    WBG.delete('closeB_P')
    root.destroy()


closeB_n = ImageTk.PhotoImage(Image.open('./1.png'))
closeB_p = ImageTk.PhotoImage(Image.open('./2.png'))
WBG.create_image(999, 1, image=closeB_n, anchor='ne', tag='closeB_N')
WBG.tag_bind('closeB_N', '<Button-1>', ClosePress)
WBG.tag_bind('closeB_N', '<ButtonRelease-1>', Winquit)


# 最小化
def MiniPress(event=None):
    WBG.create_image(929, 1, image=miniB_p, anchor='ne', tag='miniB_P')


c = 0
smroot = False


def Winminimize(event=None):
    global smroot
    WBG.delete('miniB_P')
    WBG.tag_unbind('miniB_N', '<Button-1>')
    WBG.tag_unbind('miniB_N', '<ButtonRelease-1>')
    linshi_window = tk.Toplevel(root)
    linshi_window.attributes("-alpha", 0)
    linshi_window.update()
    window_gradually_(mode='hide')
    root.overrideredirect(False)
    root.iconify()
    root.bind('<Map>', reWinminimize)
    linshi_window.destroy()
    del linshi_window
    smroot = True
    WBG.tag_bind('miniB_N', '<Button-1>', MiniPress)
    WBG.tag_bind('miniB_N', '<ButtonRelease-1>', Winminimize)
    micro_sleep(0.05)


def reWinminimize(event=None):
    global c
    c += 1
    if c == 2:
        linshi_window = tk.Toplevel(root)
        linshi_window.attributes("-alpha", 0)
        root.unbind('<Map>')
        root.deiconify()
        linshi_window.update()
        root.overrideredirect(True)
        c = 0
        root.after(10, lambda: set_appwindow(root=root))
        window_gradually_()
        linshi_window.destroy()
        del linshi_window
    micro_sleep(0.05)


miniB_n = ImageTk.PhotoImage(Image.open('./1.png'))
miniB_p = ImageTk.PhotoImage(Image.open('./2.png'))
WBG.create_image(929, 1, image=miniB_n, anchor='ne', tag='miniB_N')
WBG.tag_bind('miniB_N', '<Button-1>', MiniPress)
WBG.tag_bind('miniB_N', '<ButtonRelease-1>', Winminimize)


# 渐显窗口
def show_window():
    window_gradually_()


#root.after(50, show_window_callback)

root.mainloop()